package cn.pn.project.controller;

import cn.pn.project.client.commodity.SkuFeign;
import cn.pn.project.client.schedule.CommoditySessionFeign;
import cn.pn.project.client.schedule.RestSeatClient;
import cn.pn.project.client.schedule.SeatcommodityFeign;
import cn.pn.project.common.DtoUtil;
import cn.pn.project.pojo.commodity.Sku;
import cn.pn.project.pojo.schedule.Commoditysession;
import cn.pn.project.pojo.schedule.Seat;
import cn.pn.project.pojo.schedule.Seatcommodity;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/seatCommodity")
public class SeatcommodityController {

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SeatcommodityFeign seatcommodityFeign;

    @Autowired
    private CommoditySessionFeign commoditySessionFeign;

    @Autowired
    private RestSeatClient restSeatClient;

    /**
     * 根据skuid查询座位信息(根据场地id和场次时间查询场次id,根据场次id和商品id查询skuid,根据skuid查询座位状态具体的座位，根据座位id查询座位信息)
     * @param
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/p/SeatOccupancy",method = RequestMethod.POST)
    public Object SeatOccupancy(@RequestParam("commdityId") Integer commdityId, @RequestParam("placeId") Integer placeId,@RequestParam("sessionTime") String sessionTime) throws SQLException {
        System.out.println("==================================================sessionTime="+sessionTime);
        if (commdityId == null) {
                return DtoUtil.returnSuccess("商品id为空", "5001");
            }
            if (placeId == null) {
                return DtoUtil.returnSuccess("场地id为空", "5002");
            }
            if (sessionTime == null) {
                return DtoUtil.returnSuccess("场次时间为空", "5003");
            }
            //根据场地id和具体的场次时间查询场次id，唯一的一个场次id
            Commoditysession commoditysession =commoditySessionFeign.getCommoditysessionByPlaceIdAndSessionTime(placeId, sessionTime);
            System.out.println("===================="+commoditysession.getSessionId());
            //根据场次id和商品id查询skuid，多个
            List<Sku> skuList = skuFeign.getSkuByCommdityIdAndSessionId(commdityId, commoditysession.getSessionId());
            String skuId = "";
            for(int i=0;i<skuList.size();i++){
                if(skuList.size()>1){
                    skuId+=skuList.get(i).getSkuId()+(i<skuList.size()-1?",":"");
                }else{
                    skuId=skuList.get(i).getSkuId().toString();
                }
            }
            //根据skuid获取座位售卖情况
            List<Seatcommodity> seatcommodityList =seatcommodityFeign.getCommodityBySkuId(skuId);
             System.out.println(JSON.toJSONString(seatcommodityList, true));
            String seatId="";
            for (int i=0;i<seatcommodityList.size();i++){
                if(seatcommodityList.size()>1){
                    seatId+=seatcommodityList.get(i).getSeatId()+(i<seatcommodityList.size()-1?",":"");
                }else{
                    seatId=seatcommodityList.get(i).getSeatId().toString();
                }
            }
            //根据座位id查询座位信息
            if(seatId!=""){
                List<Seat> seatList=restSeatClient.getAllSeatInfo(seatId);
                System.out.println(JSON.toJSONString(seatList,true));
                return DtoUtil.returnSuccess("获取成功",seatList);//JSON.toJSONString(,true);
                //return "SeatCallBack(" + JSON.toJSONString(seatList,true) + ")";
            }else {
                //return "SeatCallBack("+null+")";
                return DtoUtil.returnSuccess("没有数据","5000");
            }

        }
}
